//
//  XJSeckillGoodsViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSeckillGoodsViewController.h"
#import "XJSecKillGoodsDetailViewController.h"



//view
#import "XJSecKillGoodsView.h"
//model
#import "SeckillGoodsModel.h"


@interface XJSeckillGoodsViewController ()

@property (nonatomic, strong) XJSecKillGoodsView *secKillGoodsView;

@end

@implementation XJSeckillGoodsViewController

#pragma mark - 一 lazy
- (XJSecKillGoodsView *)secKillGoodsView {
    if (!_secKillGoodsView) {
        _secKillGoodsView = [[XJSecKillGoodsView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight - XJBottomTabH)];
        
        XJWeakSelf(self);
        _secKillGoodsView.xjsecKillGoodsViewClickItemBlock = ^(SeckillGoodsModel *secKillModel) {
            XJLog(@"点击的秒杀model:%@",secKillModel);
            XJSecKillGoodsDetailViewController *secKillVC = [[XJSecKillGoodsDetailViewController alloc] init];
            secKillVC.goodsId = [NSString stringWithFormat:@"%@",secKillModel.secKillGoodsId];
            [weakself.navigationController pushViewController:secKillVC animated:YES];
        };
    }
    return _secKillGoodsView;
}

#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"附近秒杀圈";

    [self.view addSubview:self.secKillGoodsView];
    
    [self requestSecKissList];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    
}

#pragma mark - 三 网络请求
//http://xinjiangapi.zertone1.com/api/v1/home/seckill_list?lat=30.194887&lng=120.189538&page=1&page_size=10

- (void)requestSecKissList {
    
    NSString *lat = self.lat;
    NSString *lng = self.lng;
    if([NSString isEmptyString:lat]) return;
    if([NSString isEmptyString:lng]) return;

    NSString *page = @"1";
    NSString *page_size = @"10";
    
    NSDictionary *parDic = NSDictionaryOfVariableBindings(lat,lng,page,page_size);
    
    [XJHttpTool requestHomeSecKillWithParDic:parDic successH:^(id responseObjc) {

        NSMutableArray *newArr = [NSMutableArray array];
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
    
            NSArray * seckill_list = responseObjc[@"data"][@"seckill_list"];
            for (NSDictionary *dic in seckill_list) {
                SeckillGoodsModel *model = [SeckillGoodsModel mj_objectWithKeyValues:dic];
                [newArr addObject:model];
            }
        }
        
        self.secKillGoodsView.dataArray = newArr;
        
    } failH:^(NSError *error) {
        
    }];
}

- (void)dealloc {
    XJLog(@"秒杀商品vc被销毁");
    
    //销毁定时器。 secKillGoodsView 才会被销毁掉。
    [self.secKillGoodsView xjskGoodsViewInvalidTime];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
